package com.hb.trade.api.routine.service;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.center.sys.entity.DistrictInfo;
import com.hb.core.pojo.enums.CommonTypeEnum;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateDTO;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.proj.enums.BidWayEnum;
import com.hb.core.pojo.project.portal.dto.PortalNoticeDTO;
import com.hb.core.pojo.project.portal.entity.PortalNoticeFile;
import com.hb.trade.client.center.DictionaryClient;
import com.hb.trade.client.project.*;
import org.apache.commons.lang3.StringUtils;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理推送门户数据
 *
 * @author tian
 * @date 2022年05月25日 16:32
 */
@Service
public class PortalNoticeService {


    protected final PortalNoticeClient portalNoticeClient;
    protected final DictionaryClient dictionaryClient;
    protected final NoticeClient noticeClient;
    protected final ProjectClient projectClient;
    protected final DealClient dealClient;
    protected final BidWinResultClient bidWinResultClient;

    public PortalNoticeService(PortalNoticeClient portalNoticeClient,
                               DictionaryClient dictionaryClient,
                               NoticeClient noticeClient,
                               ProjectClient projectClient,
                               DealClient dealClient,
                               BidWinResultClient bidWinResultClient) {
        this.portalNoticeClient = portalNoticeClient;
        this.dictionaryClient = dictionaryClient;
        this.noticeClient = noticeClient;
        this.projectClient = projectClient;
        this.dealClient = dealClient;
        this.bidWinResultClient = bidWinResultClient;
    }

    /**
     * 保存门户的公告信息
     *
     * @author tian
     * @date 2022-5-26 10:47
     * @param noticeId 公告id
     * @param data 招标项目信息
     */
    public void pushNoticeToPortal(Long noticeId, AbstractProject data) {
        // 查询公告与招标项目信息
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setId(noticeId);
        Notice notice = noticeClient.findNoticeById(noticeDTO).getData();

        // 组装推送门户公告数据
        PortalNoticeDTO portalNoticeDTO = new PortalNoticeDTO();
        portalNoticeDTO.setProjectModule(data.getProjectModule());
        portalNoticeDTO.setProjectId(data.getId().toString());
        portalNoticeDTO.setProjectName(data.getProjectName());

        // 设置省市区
        DistrictInfo provinceInfo = dictionaryClient.districtsById(data.getProvince());
        if (provinceInfo != null) {
            portalNoticeDTO.setProvinceName(provinceInfo.getDisName());
        }
        if (StringUtils.isNotEmpty(data.getCity())) {
            DistrictInfo cityInfo = dictionaryClient.districtsById(data.getCity());
            if (cityInfo != null) {
                portalNoticeDTO.setCityName(cityInfo.getDisName());
            }
        }

        if (StringUtils.isNotEmpty(data.getArea())) {
            DistrictInfo areaInfo = dictionaryClient.districtsById(data.getArea());
            if (areaInfo != null) {
                portalNoticeDTO.setAreaName(areaInfo.getDisName());
            }
        }

        portalNoticeDTO.setNoticeTitle(notice.getNoticeTitle());
        portalNoticeDTO.setBidWay(data.getBidWay());
        portalNoticeDTO.setProjectType(data.getProjectType());
        portalNoticeDTO.setNoticeNature(notice.getNoticeNature());
        // 公告类型（1，招标/预审公告，2，中标候选人公示，3，中标结果公示）
        portalNoticeDTO.setNoticeType(1);
        portalNoticeDTO.setOrgId(data.getOrgId());
        portalNoticeDTO.setOrgName(data.getAgencyOrganizeInfo().getName());
        portalNoticeDTO.setDeployTime(notice.getDeployTime());
        if (data.getSignUpFlag().equals(1)) {
            portalNoticeDTO.setSignEndTime(notice.getSectionOptions().getSignEndTime());
            portalNoticeDTO.setSignStartTime(notice.getSectionOptions().getSignStartTime());
        }
        portalNoticeDTO.setOpenBidTime(notice.getSectionOptions().getOpenBidTime());

        // 添加公示内容
        portalNoticeDTO.setNoviceContent(notice.getNoticeContent());
        // 添加公告附件
        List<PortalNoticeFile> portalNoticeFileList = new ArrayList<>();
        PortalNoticeFile portalNoticeFile;
        if (CollUtil.isNotEmpty(notice.getAttachment())) {
            for (Attachment attachment : notice.getAttachment()) {
                portalNoticeFile = new PortalNoticeFile();
                portalNoticeFile.setFileName(attachment.getRawFileName() + "." + attachment.getExtName());
                portalNoticeFile.setFilePath(attachment.getFileUrl());
                portalNoticeFileList.add(portalNoticeFile);
            }
        }
        // 添加招标文件
        if (CollUtil.isNotEmpty(notice.getBidFile())) {
            for (Attachment attachment : notice.getBidFile()) {
                portalNoticeFile = new PortalNoticeFile();
                portalNoticeFile.setFileName(attachment.getRawFileName() + "." + attachment.getExtName());
                portalNoticeFile.setFilePath(attachment.getFileUrl());
                portalNoticeFileList.add(portalNoticeFile);
            }
        }
        portalNoticeDTO.setPortalNoticeFileList(portalNoticeFileList);

        portalNoticeClient.savePortalNotice(portalNoticeDTO);
    }


    /**
     * 保存门户的中标候选人信息
     *
     * @author tian
     * @date 2022-5-26 10:45
     * @param cidCandidateId 中标候选人公示id
     */
    public void pushCandidateToPortal(Long cidCandidateId) {
        // 获取中标候选人公示和招标项目信息
        BidCandidateDTO bidCandidateDTO = new BidCandidateDTO();
        bidCandidateDTO.setId(cidCandidateId);
        Message<BidCandidateDTO> candidateDetail = dealClient.getCandidateDetail(bidCandidateDTO);

        AbstractProject data = new AbstractProject();
        if(candidateDetail.isSuccess()){
            bidCandidateDTO = candidateDetail.getData();
            data.setId(candidateDetail.getData().getProjectId());
            data = projectClient.projectDetail(data).getData();
        }

        // 如果是公开的或者是单一来源的则需要同步到门户公告
        if(Enums.equals(CommonTypeEnum.OPEN,data.getCommonType()) || Enums.equals(BidWayEnum.SINGLE,data.getBidWay())){
            // 组装推送门户公告数据
            PortalNoticeDTO portalNoticeDTO = new PortalNoticeDTO();
            portalNoticeDTO.setProjectModule(data.getProjectModule());
            portalNoticeDTO.setProjectId(data.getId().toString());
            portalNoticeDTO.setProjectName(data.getProjectName());

            // 设置省市区
            DistrictInfo provinceInfo = dictionaryClient.districtsById(data.getProvince());
            if (provinceInfo != null) {
                portalNoticeDTO.setProvinceName(provinceInfo.getDisName());
            }
            DistrictInfo cityInfo = dictionaryClient.districtsById(data.getCity());
            if (cityInfo != null) {
                portalNoticeDTO.setCityName(cityInfo.getDisName());
            }
            DistrictInfo areaInfo = dictionaryClient.districtsById(data.getArea());
            if (areaInfo != null) {
                portalNoticeDTO.setAreaName(areaInfo.getDisName());
            }

            portalNoticeDTO.setNoticeTitle(bidCandidateDTO.getPublicityName());
            portalNoticeDTO.setBidWay(data.getBidWay());
            portalNoticeDTO.setProjectType(data.getProjectType());
            portalNoticeDTO.setNoticeNature(bidCandidateDTO.getPublicityType());
            // 公告类型（1，招标/预审公告，2，中标候选人公示，3，中标结果公示）
            portalNoticeDTO.setNoticeType(2);
            portalNoticeDTO.setOrgId(data.getOrgId());
            portalNoticeDTO.setOrgName(data.getAgencyOrganizeInfo().getName());
            portalNoticeDTO.setDeployTime(bidCandidateDTO.getDeployTime());

            // 添加公示内容
            portalNoticeDTO.setNoviceContent(bidCandidateDTO.getNoticeContent());
            // 添加公告附件
            List<PortalNoticeFile> portalNoticeFileList = new ArrayList<>();
            PortalNoticeFile portalNoticeFile;
            if (!bidCandidateDTO.getAttachment().isEmpty()) {
                for (Attachment attachment : bidCandidateDTO.getAttachment()) {
                    portalNoticeFile = new PortalNoticeFile();
                    portalNoticeFile.setFileName(attachment.getRawFileName() + "." + attachment.getExtName());
                    portalNoticeFile.setFilePath(attachment.getFileUrl());
                    portalNoticeFileList.add(portalNoticeFile);
                }
            }
            portalNoticeDTO.setPortalNoticeFileList(portalNoticeFileList);

            portalNoticeClient.savePortalNotice(portalNoticeDTO);
        }
    }



    /**
     * 保存门户的中标公示信息
     *
     * @author tian
     * @date 2022-5-26 10:45
     * @param widResultId 结果公示id
     */
    public void pushWidResultToPortal(Long widResultId) {
        // 获取中标候选人公示和招标项目信息
        BidWinResultDTO bidWinResultDTO = new  BidWinResultDTO ();
        bidWinResultDTO.setId(widResultId);
        Message<BidWinResultDTO> bidWinResultDTOMessage = bidWinResultClient.bidWinResultInfoDetail(bidWinResultDTO);

        AbstractProject data = new AbstractProject();
        if(bidWinResultDTOMessage.isSuccess()){
            bidWinResultDTO = bidWinResultDTOMessage.getData();
            data.setId(bidWinResultDTO.getProjectId());
            data = projectClient.projectDetail(data).getData();
        }

        // 如果是公开的或者是单一来源的则需要同步到门户公告
        if(Enums.equals(CommonTypeEnum.OPEN,data.getCommonType()) || Enums.equals(BidWayEnum.SINGLE,data.getBidWay())){
            // 组装推送门户公告数据
            PortalNoticeDTO portalNoticeDTO = new PortalNoticeDTO();
            portalNoticeDTO.setProjectModule(data.getProjectModule());
            portalNoticeDTO.setProjectId(data.getId().toString());
            portalNoticeDTO.setProjectName(data.getProjectName());

            // 设置省市区
            DistrictInfo provinceInfo = dictionaryClient.districtsById(data.getProvince());
            if (provinceInfo != null) {
                portalNoticeDTO.setProvinceName(provinceInfo.getDisName());
            }
            DistrictInfo cityInfo = dictionaryClient.districtsById(data.getCity());
            if (cityInfo != null) {
                portalNoticeDTO.setCityName(cityInfo.getDisName());
            }
            DistrictInfo areaInfo = dictionaryClient.districtsById(data.getArea());
            if (areaInfo != null) {
                portalNoticeDTO.setAreaName(areaInfo.getDisName());
            }

            portalNoticeDTO.setNoticeTitle(bidWinResultDTO.getNoticeTitle());
            portalNoticeDTO.setBidWay(data.getBidWay());
            portalNoticeDTO.setProjectType(data.getProjectType());
            portalNoticeDTO.setNoticeNature(bidWinResultDTO.getNoticeNature());
            // 公告类型（1，招标/预审公告，2，中标候选人公示，3，中标结果公示）
            portalNoticeDTO.setNoticeType(3);
            portalNoticeDTO.setOrgId(data.getOrgId());
            portalNoticeDTO.setOrgName(data.getAgencyOrganizeInfo().getName());
            portalNoticeDTO.setDeployTime(bidWinResultDTO.getDeployTime());

            // 添加公示内容
            portalNoticeDTO.setNoviceContent(bidWinResultDTO.getNoticeContent());
            // 添加公告附件
            List<PortalNoticeFile> portalNoticeFileList = new ArrayList<>();
            PortalNoticeFile portalNoticeFile;
            if (!bidWinResultDTO.getAttachment().isEmpty()) {
                for (Attachment attachment : bidWinResultDTO.getAttachment()) {
                    portalNoticeFile = new PortalNoticeFile();
                    portalNoticeFile.setFileName(attachment.getRawFileName() + "." + attachment.getExtName());
                    portalNoticeFile.setFilePath(attachment.getFileUrl());
                    portalNoticeFileList.add(portalNoticeFile);
                }
            }
            portalNoticeDTO.setPortalNoticeFileList(portalNoticeFileList);

            portalNoticeClient.savePortalNotice(portalNoticeDTO);
        }
    }
}
